package microBlogger.screens;

import context.*;

import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Ticker;

import microBlogger.AccountRecordStore;
import microBlogger.MicroBlogger;

import bloggerAPI.j2me.BloggerConnection;


// ******************************************************************
// *** CLASS: MainScreen
// ******************************************************************
public class MainScreen extends Alert implements Debug.Constants
{ //private static final boolean DEBUG=true; // Overriding Debug.Constants.DEBUG
  private static final Command connectCommand=new Command("Connect",Command.OK,0);
  private static final Command accountCommand=new Command("Account",Command.EXIT,1);
  
  private MicroBlogger midlet;
  private AccountRecordStore account;

  public MainScreen(final MicroBlogger midlet)
  { super("Micro Blogger");
    try { setImage(Image.createImage("/microBlogger/logo.png")); }
    catch (IOException e) { setString("Micro Blogger"); }
    this.midlet=midlet;
    this.account=new AccountRecordStore();    

    this.addCommand(connectCommand);
    this.addCommand(accountCommand);
    this.setCommandListener(new CommandListener()
    { public void commandAction(Command c,Displayable s)
      { if(c==connectCommand) connect(); 
    	else if(c==accountCommand) showAccountScreen();
      }
    });
  }
  
  
  private void showAccountScreen()
  { new AccountScreen(midlet,account).show(); }
  
  private void connect()
  { if(DEBUG) Debug.println("MicroBlogger.connect: ");            
    // *** should ensure it is disconnected...
	new Thread("Connecting")
  	{ public void run()
  	  { try
  	    { setTicker(new Ticker("... connecting ..."));
          BloggerConnection bloggerConnection=new BloggerConnection
  	          ("https://www.blogger.com/atom",account.getUserID(),account.getPassword());
  	      BlogListScreen blogListScreen=new BlogListScreen(midlet,bloggerConnection);	
  	      blogListScreen.show();
  	    }
  	    catch(Exception e) { Debug.EXCEPTION("MicroBlogger.connect",e); }
  	  }
  	}.start();
  }

}